import random

import pygame

"""
 配置常量
"""
# SCREE_RECT = pygame.Rect(SCREEN_WIDTH, SCREEN_HIGHT, 0, 0)
# 敌机速度
SPEED_MIN = 1
SPEED_MAX = 3
# 敌机创建事件的间隔时间
ENEMY_CREATE_TIME = 500

# 英雄移动速度
HERO_SPEED = 1

"""
图片地址区
方便后续改配置方案
"""
# 图片背景地址
BACKGROUND_IMAGE = "./images/background.png"
# 英雄飞机
HERO_IMAGE_1 = "./images/me1.png"
HERO_IMAGE_2 = "./images/me2.png"

# 敌人飞机
ENEMY_IMAGE_1 = "./images/enemy1.png"
ENEMY_IMAGE_2 = "./images/enemy2.png"

# 子弹
HERO_BULLET = "./images/bullet1.png"
ENEMY_BULLET = "./images/bullet2.png"

ENEMY_CRASH_IMAGE_1 = "./images/enemy1_down{}.png"
ENEMY_CRASH_IMAGE_2 = "./images/enemy2_down{}.png"
HERO_CRASH_IMAGE = "./images/me_destroy_{}.png"

"""
 定时器常量
"""
# 创建敌机定时器常量
CREATE_ENEMY_EVENT = pygame.USEREVENT
# 创建发射子弹英雄定时器常量
HERO_FIRE_EVENT = pygame.USEREVENT + 1


# 游戏精灵类
class GameSprite(pygame.sprite.Sprite):
    """
    游戏精灵
    """

    def __init__(self, image_name, speed=1):
        # 调用父类初始化方法
        super().__init__()

        # 定义对象属性
        self.image = pygame.image.load(image_name)
        self.speed = speed
        self.rect = self.image.get_rect()
        (self.width, self.height) = pygame.display.get_window_size()

    def update(self):
        """
        移动
        :return:
        """

        self.rect.y += self.speed

        # if self.rect.y >= self.height:
        #     self.rect.y = 0


# 背景精灵
class BackgroundSprite(GameSprite):
    """
    游戏背景精灵
    """

    def __init__(self, image_name=BACKGROUND_IMAGE, space=0):
        """
        初始化背景

        :param image_name: 图片地址
        :param space: 图片Y轴移动倍数 默认：0,可以为负数
        """
        # 调用父类初始化方法
        super().__init__(image_name)
        self.rect.y = space * self.rect.height

    def update(self):
        super().update()

        if self.rect.y >= self.height:
            self.rect.y = -self.rect.height


# 敌机精灵
class Enemy(GameSprite):
    """
    敌机精灵
    """

    def __init__(self, image_name=ENEMY_IMAGE_1):
        pass
        # 1. 调用父类方法，创建敌机精灵，指定敌机图片
        super().__init__(image_name)
        # 2.指定敌机的初始随机速度
        self.speed = random.randint(SPEED_MIN, SPEED_MAX)
        # 3. 指定敌机的初始随机位置
        self.rect.bottom = 0
        self.rect.x = random.randint(0, self.width - self.rect.width)

    def update(self):
        # 1. 调用父类方法，保持垂直方向飞行
        super().update()
        # 2.判断是否飞出屏幕，如果是，需要从精灵组删除敌机

        if self.rect.y >= self.height:
            # print("敌机飞出屏幕外。")
            self.kill()
        pass

    def __del__(self):
        # print("敌机被销毁...%s" % self.rect)
        pass


# 英雄精灵
class Hero(GameSprite):
    """
    英雄精灵类
    """

    def __init__(self, image_name=HERO_IMAGE_1):
        # 1. 调用父类方法，创建敌机精灵，指定敌机图片
        super().__init__(image_name)
        self.speed = HERO_SPEED

        # 3. 指定初始位置
        self.rect.bottom = self.height - 120
        self.rect.x = int((self.width - self.rect.width) / 2)

        self.heroBulletGroup = None

        pygame.time.set_timer(HERO_FIRE_EVENT, 100)

        self.heroBulletGroup = pygame.sprite.Group()
        pass

    def update(self):
        self.rect.x += self.speed
        if self.rect.x > self.width:
            self.rect.x = self.width
        elif self.rect.x < 0:
            self.rect.x = 0
        pass

    # 控制水平移动
    def set_speed(self, keys=0):
        if keys == pygame.K_RIGHT:
            self.speed = HERO_SPEED
        elif keys == pygame.K_LEFT:
            self.speed = -HERO_SPEED
        else:
            self.speed = 0

    def fire(self):
        # print("发射子弹")

        # 创建英雄子弹组
        hero_bullet = Bullet(self, HERO_BULLET)
        self.heroBulletGroup.add(hero_bullet)

    def kill(self):
        super().kill()

    def __del__(self):
        print("英雄挂了..")


# 子弹类
class Bullet(GameSprite):
    """
    子弹精灵类

    """

    def __init__(self, hero, image=HERO_BULLET):
        if not hero:
            return

        # 父类初始化
        super().__init__(image)

        # 紧跟英雄坐标中位数
        self.hero = hero
        self.rect.centerx = self.hero.rect.centerx
        self.rect.centery = self.hero.rect.centery

    def update(self):
        # 子弹飞
        self.rect.y -= self.speed
        if self.rect.y < 0:
            self.kill()

    def __del__(self):
        # print("子弹销毁")
        pass
